package org.example.service.impl;

import org.example.exception.ClazzHasException;
import org.example.exception.ClazzHasException;
import org.example.mapper.DeptMapper;
import org.example.pojo.Dept;
import org.example.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService{
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> findAll(){
        List<Dept> all = deptMapper.findAll();
        return all;
    }

    @Override
    public void deleteById(Integer id){
//        如果部门下有员工，则不允许删除该部门，并给前端提示错误信息：对不起，当前部门下有员工，不能直接删除！
        Integer i =  deptMapper.countById(id);
        if (i>0){
            throw new ClazzHasException("对不起，当前部门下有员工，不能直接删除！");
        }else {
            deptMapper.deleteById(id);
        }
    }
    @Override
    public void add(Dept dept){
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getById(Integer id){
        Dept dept = deptMapper.getById(id);
        return dept;
    }

    @Override
    public void update(Dept dept){
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
